package com.sunday.aomicaichang.adpter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.sunday.common.widgets.CircleImageView;
import com.sunday.aomicaichang.R;
import com.sunday.aomicaichang.model.Assess;
import com.sunday.aomicaichang.widgets.FlexibleRatingBar;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by yun_sheng on 2017/6/7.
 */

public class AssessAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private Context mContext;
    private List<Assess> assessList;

    public AssessAdapter(Context mContext) {
        this.mContext = mContext;

    }

    public void setmData(List<Assess> assessList) {
        this.assessList = assessList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_goods_assess, parent, false);
        return new ItemViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final Assess assess = assessList.get(position);
        ItemViewHolder viewHolder = (ItemViewHolder) holder;
        if (assess.getMemberImage() != null)
            Picasso.with(mContext)
                    .load(assess.getMemberImage())
                    .error(R.mipmap.ic)
                    .into(viewHolder.imgTop);
        if (assess.getMemberName() != null)
            viewHolder.tvName.setText(assess.getMemberName());
        if (assess.getCreateDate() != null)
            viewHolder.tvTime.setText(assess.getCreateDate());
        if (assess.getContent() != null)
            viewHolder.assess.setText(assess.getContent());
        List<Assess.ImagesBean> imagesBeen = assess.getImages();
        viewHolder.lyImg.removeAllViews();
        if (imagesBeen.size() != 0) {
            for (Assess.ImagesBean i : imagesBeen) {
                    View v1 = LayoutInflater.from(mContext).inflate(R.layout.item_assess_img, null);
                    ImageView area = (ImageView) v1.findViewById(R.id.img);
                    Picasso.with(mContext)
                            .load(i.getUrl())
                            .error(R.mipmap.ic_product)
                            .into(area);
                    viewHolder.lyImg.addView(v1);
            }
        }
        viewHolder.flexibleRatingBar.setRating(assess.getScore());

    }

    @Override
    public int getItemCount() {
        return assessList == null ? 0 : assessList.size();
    }


    public class ItemViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.img_top)
        CircleImageView imgTop;
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.flexibleRatingBar)
        FlexibleRatingBar flexibleRatingBar;
        @Bind(R.id.tv_time)
        TextView tvTime;
        @Bind(R.id.assess)
        TextView assess;
        @Bind(R.id.ly_img)
        LinearLayout lyImg;
        public ItemViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

}
